package com.shaunstone.gae_web.controller;

import java.util.ArrayList;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.shaunstone.gae.workout.model.BodyParts;
import com.shaunstone.gae.workout.model.Exercise;
import com.shaunstone.gae.workout.service.ExerciseService;

@Controller
public class ExerciseController {

	@Autowired
	private ExerciseService exerciseService;

	@RequestMapping(value = "exercise", method = RequestMethod.GET)
	public ModelAndView exercisePage() {

		Collection<Exercise> exercises = exerciseService.getExercise(null);
		Collection<BodyParts> bps = new ArrayList<BodyParts>();
		for (Exercise exercise : exercises) {
			bps.add(exercise.getPrimaryBodyParts());
		}
		ModelAndView mView = new ModelAndView("exercise");
		mView.addObject("exercise", new Exercise());
		mView.addObject("bps", BodyParts.values());
		mView.addObject("exercises", exercises);
		return mView;
	}

	@RequestMapping(value = "exercise", method = RequestMethod.POST)
	public String addExcer(@ModelAttribute("exercise") Exercise exercise) {
		exerciseService.addExercise(exercise);
		return "redirect:exercise";
	}

	public void setExerciseService(ExerciseService exerciseService) {
		this.exerciseService = exerciseService;
	}
}
